package com.midea.donenesstool.analysis.controller;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@RestController
public class AnalysisController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Value("${specificSn}")
	private String specificSn;
	
	@Value("${savePicPath}")
	private String savePicPath;
	
	@RequestMapping("/done/upload")
	public JSONObject upload(MultipartFile image,
					     String sn,
					     double probe_temperature,
					     int num) {
		logger.info("sn:{},probe_temperature:{},num:{}", sn, probe_temperature, num);
		
//		if(specificSn.equals(sn)) { //指定演示sn保存图片
			String path = savePicPath + File.separator + sn + ".jpg";
			File file = new File(path);
			try {
				FileUtils.writeByteArrayToFile(file, image.getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}
//		}
		
		String sql = "select label, doneness from doneness_fake where id = ?";
		Map<String, Object> result = jdbcTemplate.queryForMap(sql, new Object[] {"1"});
		
		JSONObject response = new JSONObject();
		response.put("errorCode", "0");
		response.put("msg", "");
		response.put("result", result);
		
//		String resp = JSON.toJSONString(response, true);
//		logger.info("{}", resp);
		
		return response;
	}
	
}
